package editor;

import game.Livello;
import grafica.Myframe;

import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class DialogNuovoLivello extends JDialog implements ActionListener{
	private Toolkit tool;
	private EditorFrame editorFrame;
	private JTextField testo;
	private JLabel label;
	private JLabel error;
	private JButton conferma;
	private int lungh;
	
	public DialogNuovoLivello (EditorFrame editorFrame){
		tool = Toolkit.getDefaultToolkit();
		this.editorFrame = editorFrame;
		lungh = 0;
		testo = new JTextField(4);
		label = new JLabel("Inserisci lunghezza livello");
		error = new JLabel("");
		conferma = new JButton("Ok");
		conferma.addActionListener(this);
		this.setLayout(new FlowLayout());
		this.add(label);
		this.add(testo);
		this.add(conferma);
		this.add(error);
		this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		this.setTitle("Nuovo livello");
		this.setSize(250,130);
		this.setLocation(500,300);
		this.setAlwaysOnTop(true);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == conferma){
			if (!testo.getText().isEmpty() && testo.getText().matches("[0-9]+")){
				this.lungh = Integer.valueOf(testo.getText()).intValue();
				if(lungh*Livello.TILE_SIZE > Myframe.SCREEN_WIDTH){
					editorFrame.rimuoviTutto();
					editorFrame.impostaPannelli(lungh,12);
					editorFrame.getChoose().abilitaScelta(true);
					editorFrame.getEditor().setModificato(true);
					this.dispose();
				}
				else{
					error.setIcon(new ImageIcon(tool.getImage("editor"+File.separator+"icon"+File.separator+"alt.png")));
					error.setText(" Lunghezza del livello non valida. ");
				}
			}
			else{
				error.setIcon(new ImageIcon(tool.getImage("editor"+File.separator+"icon"+File.separator+"alt.png")));
				error.setText(" Valori immessi non validi. ");
			}
		}
	}
}
